package cn.new7as.cas5s.ui.slideshow;

import static com.lxj.xpopup.util.XPopupUtils.dp2px;
import static com.lxj.xpopup.util.XPopupUtils.getStatusBarHeight;

import android.os.Bundle;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

import java.util.Objects;

import cn.new7as.cas5s.R;
import cn.new7as.cas5s.tool.util.SharePreferenceUtils;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.TokenException;
import cn.new7as.cas5s.ui.login.LogininActivity;
import cn.new7as.cas5s.ui.record.RecordsActivity;


public class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        start=System.currentTimeMillis();
        UltimateBarX.statusBarOnly(this)
                .fitWindow(false)
                .colorRes(R.color.tran)
                .light(true)
                .lvlColorRes(R.color.tran)
                .apply();
    }

  private   String useruniquecode;
  public    long start;

    public String getunique() {
            useruniquecode = new SharePreferenceUtils(this).getString("useruniquecode", "");
        return useruniquecode;
    }


    public void toa(String s) {
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               View layout = View.inflate(BaseActivity.this, R.layout.layout_toast, null);

               TextView textView1 = layout.findViewById(R.id.textView1);
               textView1.setText(s);



               Toast toast = new Toast(getApplicationContext());
               toast.setDuration(Toast.LENGTH_LONG);
               toast.setView(layout);
               toast.show();

           }
       });


    }


    public void initV() {

    }

    public void setFullscreen(boolean isShowStatusBar, boolean isShowNavigationBar) {
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        if (!isShowStatusBar) {
            uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
        }
        if (!isShowNavigationBar) {
            uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
        getWindow().getDecorView().setSystemUiVisibility(uiOptions);

    }


    public void setop(String tt) {
        View inflate = View.inflate(this, R.layout.layout_common_tittle, null);
        inflate.findViewById(R.id.backsds).setOnClickListener(v -> finish());
        ((TextView) inflate.findViewById(R.id.commontittle)).setText(tt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        inflate.setLayoutParams(params);
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        int top = getStatusBarHeight() + dp2px(this, 5);
        inflate.setBackgroundResource(R.drawable.alltran);
        View viewById = decorView.findViewById(android.R.id.content);
        viewById
                .setBackgroundResource(R.drawable.sameback);
        inflate.setPadding(0, top, 0, 0);

        decorView.addView(inflate);
    }

    public void setop(String tt, View.OnClickListener clickListener) {
        View inflate = View.inflate(this, R.layout.layout_common_tittle, null);
        inflate.findViewById(R.id.backsds).setOnClickListener(clickListener);
        ((TextView) inflate.findViewById(R.id.commontittle)).setText(tt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        inflate.setLayoutParams(params);
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        int top = getStatusBarHeight() + dp2px(this, 5);
        inflate.setBackgroundResource(R.drawable.alltran);
        View viewById = decorView.findViewById(android.R.id.content);
        viewById
                .setBackgroundResource(R.drawable.sameback);
        inflate.setPadding(0, top, 0, 0);

        decorView.addView(inflate);
    }

    public void setop(String tt, Boolean a) {
        View inflate = View.inflate(this, R.layout.layout_common_tittle, null);
        inflate.findViewById(R.id.backsds).setOnClickListener(v -> finish());
        ((TextView) inflate.findViewById(R.id.commontittle)).setText(tt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        inflate.setLayoutParams(params);

        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        int top = getStatusBarHeight() + dp2px(this, 5);
        if (a) {
            ((ImageView) inflate.findViewById(R.id.his)).setVisibility(View.VISIBLE);
            ((ImageView) inflate.findViewById(R.id.his)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.go(BaseActivity.this, RecordsActivity.class);
                }
            });
        }
        inflate.setPadding(0, top, 0, 0);
        decorView.addView(inflate);
    }


    public void deal(Throwable throwable) {
        hide();
        if (throwable instanceof TokenException) {
            Utils.go(this, LogininActivity.class);
            toa(getString(R.string.http_token_error));
        } else {
            if (!Objects.requireNonNull(throwable.getMessage()).isEmpty()) {
                toa(throwable.getMessage());
            }
        }
    }


    public void setEtListener(EditText... text) {
        for (int i = 0; i < text.length; i++) {
            text[i].addTextChangedListener(listener);
        }

    }

    TvListener listener = new TvListener();

    class TvListener implements TextWatcher {


        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().isEmpty())
                setFalse();
            else
                setTrue();
        }
    }

    public void setFalse() {

    }

    public void setTrue() {

    }



    LoadingPopupView loadingPopup;

    public void show() {
        if (loadingPopup == null) {
            loadingPopup = (LoadingPopupView) new XPopup.Builder(this)
                    .dismissOnBackPressed(false)
                    .dismissOnTouchOutside(false)
                    .isLightNavigationBar(true)
                    .isViewMode(true)
                    .asLoading("loading")
                    .show();
        } else {
            loadingPopup.show();
        }

    }

    public void hide() {
        if (loadingPopup != null)
            try {
                loadingPopup.dismiss();
            } catch (Exception ignored) {

            }

    }


}